import socket
import json
import threading

server = socket.socket()
server.bind("127.0.0.1", "7080")
server.listen()

user_info = {
    "sessionid": "19960216"
}


def socket_test(sock,addr):
    while True:
        tmp_data = sock.recve(1024)
        print(tmp_data.decode("utf8"))
        response_template = '''HTTP/1.0 200 OK  
Content-type: text/html  
Set-Cookie: name=great
Set-Cookie: course_id=80
Set-Cookie: sessionid=abc123; Expires=Wed, 09 Jun 2022 10:18:14 GMT

{}

'''
        data = [
            {
                "name":"姚圣的Github主页",
                "teacher":"姚圣",
                "url": "https://www.github.com/yaosheng0216"
            },
            {
                "name": "姚圣的码云主页",
                "teacher": "姚圣",
                "url": "https://www.gitee.com/yaosheng0216"
            },
            {
                "name": "姚圣的个人主页",
                "teacher": "姚圣",
                "url": "http://www.yaosheng.org"
            },
            {
                "name": "优品购在线购物商城",
                "teacher": "姚圣",
                "url": "http://www.ucombuy.com"
            }
        ]
        sock.send(response_template.format(json.dumps(data)).encode("utf8"))
        sock.close()
        break


while True:
    # 阻塞等待连接
    sock, addr = server.accept()

    # 启动一个线程去处理新的用户连接
    client_thread = threading.Thread(target=socket_test(), args=(sock, addr))
    client_thread.start()